package cn.itcast.service.impl;

import cn.itcast.clients.CastClients;
import cn.itcast.clients.NoteClients;
import cn.itcast.clients.OrderClients;
import cn.itcast.mapper.UserMapper;
import cn.itcast.entity.Note;
import cn.itcast.entity.Result;
import cn.itcast.VO.OrderUserVO;
import cn.itcast.service.UserService;
import cn.itcast.DTO.PageQueryDTO;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private OrderClients orderClients;
    @Autowired
    private CastClients castClients;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private NoteClients noteClients;

    //添加用户
    @Override
    public User add(User user) {
        userMapper.add(user);
        log.info("1111  {}",user);
        Note note = new Note();
        note.setUserId(user.getId());
        note.setBeSentPhone(user.getPhone());
        noteClients.noteAdd(note);
        return user;
    }

    //根据id查询用户
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }


    //分页查询
    @Override
    public PageResult page(PageQueryDTO dto) {
        //设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        // 查询所有用户信息
        List<User> list = userMapper.page(dto);
        if (list != null && list.size() > 0){
            for (User user : list) {
                // 根据用户ID查询消费总金额并添加到user对象里面
                Double money = castClients.getMoney(user.getId());
                log.info("222  {}",user.getId());
                user.setMoney(money);
                // 获取住房次数和信誉度
                Result<OrderUserVO> orderDayByDate = orderClients.getOrderDayByDate(user.getId());
                // 获取住房次数
                Long frequency = orderDayByDate.getData().getFrequency();
                user.setFrequency(frequency);
                // 获取信誉度
                Double cashPledgeCondition = orderDayByDate.getData().getCashPledgeCondition();
                user.setCashPledgeCondition(cashPledgeCondition);
                // 计算平均消费
                if (frequency!= null){
                    user.setEvenMoney(money/frequency);
                }
            }
        }

        Page<User> page = (Page<User>) list;

        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    //修改用户
    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    //删除用户
    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

   /* //住房次数,金额信用度
    @Override
    public void orderAdd(User user){
        Result<OrderUserVO> orderDayByDate = orderClients.getOrderDayByDate(user.getId());
        User user1 = new User();
        user1.setFrequency(orderDayByDate.getData().getFrequency());
        user1.setCashPledgeCondition(orderDayByDate.getData().getCashPledgeCondition());
        userMapper.add(user1);
    }

    //消费金额总计,平均消费
    @Override
    public void castAdd(User user){
        Double money = castClients.getMoney(user.getId());
        User user1 = new User();
        user1.setMoney(money);
        Result<OrderUserVO> orderDayByDate = orderClients.getOrderDayByDate(user.getId());
        double v = (money) / (orderDayByDate.getData().getFrequency());
        user1.setEvenMoney(v);
        userMapper.add(user1);
    }*/

}
